/**
*	Class: PostIt
*
* 	This class is used to represent post-its all over the widget code.
*	Beyond it's attributes, it also has usefull auxiliar methods to handle it.
*
*	@param id - the post-it's unique identifier
*	@param task - the task to be acomplished
*	@param description - the description of the task
*	@param stars - ranking of importance of this task
*	@param startDate - the date when this post-it was created
*	@param expirationDate - the estimated limit date to accomplish the task (opcional field)
*	@param finalizeDate - the date which the task was accomplished
*/
function PostIt(id, task, description, stars, startDate, expirationDate, finalizationDate){

	this._id = id;
	this._task = task;
	this._description = description;
	this._stars = stars;
	this._startDate = startDate;
	this._expirationDate = expirationDate;
	this._finalizationDate = finalizationDate;

}

// PostIt Constants

PostIt.prototype.MIN_STARS = 0;
PostIt.prototype.MAX_STARS = 3;
PostIt.prototype.NEAR_EXPIRATION = 5; // value in days

// PostIt getters
PostIt.prototype.getId = function () {
	return this._id;
};

PostIt.prototype.getTask = function () {
	return this._task;
};

PostIt.prototype.getDescription = function () {
	return this._description;
};

PostIt.prototype.getStars = function () {
	return this._stars;
};

PostIt.prototype.getStartDate = function(){
	return this._startDate;
};

PostIt.prototype.getExpirationDate = function () {
	return this._expirationDate;
};

PostIt.prototype.getFinalizationDate = function () {
	return this._finalizationDate;
};

// PostIt setters

PostIt.prototype.setId = function (id) {
	this._id = parseInt(id);			
};


PostIt.prototype.setStars = function (stars) {
	stars = parseInt(stars);
	if(typeof stars == typeof 1) //TODO put "int" literal 
		if(stars >= PostIt.prototype.MIN_STARS && stars <= PostIt.prototype.MAX_STARS)	
			this._stars = stars;
};

PostIt.prototype.setTask = function (task) {
	if(typeof task == typeof "")
		this._task = task;
};

PostIt.prototype.setDescription = function (description) {
	if(typeof description == typeof "")
		this._description = description;
};

PostIt.prototype.setStartDate = function (startDate) {
	if(typeof startDate == typeof new Date()) // TODO put "Date" literal
		this._startDate = startDate;
};

PostIt.prototype.setExpirationDate = function (expiration) {
	if(typeof expiration == typeof new Date())
		this._expirationDate = expiration;
};

PostIt.prototype.setFinalizationDate = function (finalize) {
	if(typeof finalize == typeof new Date())
		this._finalizationDate = finalize;
}; 

// Other PostIt methods

/**
*	Extract post-it into a XML format to communicate with the app. server.
*
*	@return String - the post-it in xml document format
*/
PostIt.prototype.genXml = function () { 

	var xml = new String("");
	
	xml += '<' + TAG_POST_IT + " " + TAG_ID + '="' + this._id + '">';
	xml += tag(TAG_TASK, this._task);

	if(typeof this._description != "undefined" && this._description.length > 0){
		xml += tag(TAG_DESCRIPTION, this._description);				
	}
		
	xml += tag(TAG_STARS, this._stars);

	
	xml += "<" + TAG_START_DATE + ">";			
	xml += tag(TAG_DAY, this._startDate.getDate());
	xml += tag(TAG_MONTH, this._startDate.getMonth());
	xml += tag(TAG_YEAR, this._startDate.getFullYear());
	xml += "</" + TAG_START_DATE + ">";
	

	if(typeof this._expirationDate != "undefined"){				
		xml += "<" + TAG_EXPIRATION_DATE + ">";
		xml += tag(TAG_DAY, this._expirationDate.getDate());
		xml += tag(TAG_MONTH, this._expirationDate.getMonth());
		xml += tag(TAG_YEAR, this._expirationDate.getFullYear());
		xml += "</" + TAG_EXPIRATION_DATE + ">";
	}

	if(typeof this._finalizationDate != "undefined"){
		xml += "<" + TAG_FINALIZATION_DATE + ">";
		xml += tag(TAG_DAY, this._finalizationDate.getDate());
		xml += tag(TAG_MONTH, this._finalizationDate.getMonth());
		xml += tag(TAG_YEAR, this._finalizationDate.getFullYear());
		xml += "</" + TAG_FINALIZATION_DATE + ">";
	}
	
	xml += "</" + TAG_POST_IT + ">";
	
	return xml;
};

/**
*	Says if the task has expiration date or not.
*
*	@return boolean - true case the task is expired, false case not
*/
PostIt.prototype.expires = function(){
	return (typeof this._expirationDate == typeof new Date());
};

/**
*	Says if the task is expired or not.
*
*	@return boolean - true case the task is expired, false case not
*/
PostIt.prototype.isExpired = function () { 
	if(typeof this._expirationDate == "undefined")
		return false;
	var today = new Date();
	return this.diffDays(today, this._expirationDate) > 0; // TODO check this out
};	

/**
*	Says if the task is near to be expired.
*
*	@return boolean - true case the task is near to be expired, false case not
*/
PostIt.prototype.isNearExpiration = function () {
	if(typeof this._expirationDate != "undefined" && !this.isExpired()){
		if(this.daysToExpire() <= PostIt.prototype.NEAR_EXPIRATION){
			return true;
		}
	}
	return false;
};

/**
*	Says if the task expires today.
*
*	@return boolean - true case the task expires today, false case not
*/
PostIt.prototype.expiresToday = function(){
	var today = new Date();
	if(typeof this._expirationDate != "undefined")
		return this.diffDays(this._expirationDate, today) == 0;
	else
		return false;
}

/**
*	Says if the task is finished or not.
*
*	@return boolean - true case the post-it's task was finished, false case not
*/
PostIt.prototype.isFinished = function () { 
	return (typeof this._finalizationDate != "undefined");
};

/**
*	Returns the number of days passed since the date which the task acomplisment was estimated.
*
*	@return int
*/
PostIt.prototype.daysExpired = function(){
	var today = new Date();
	if(typeof this._expirationDate != "undefined"){
		var daysExpired = this.diffDays(today, this._expirationDate);

		if(daysExpired > 0)
			return daysExpired;
		else
			return 0;
	}else
		return 0;
};
        
/**
*	Returns the number of days passed since the date which the task acomplisment was finalized.
*
*	@return int
*/
PostIt.prototype.daysFinalized = function(){
	var today = new Date();
	if(typeof this._finalizationDate != "undefined"){
		var daysFinalized = this.diffDays(today, this._finalizationDate);

		if(daysFinalized > 0)
			return daysFinalized;
		else
			return 0;
	}else
		return 0;
};

/**
*	Returns the number of days which the task will be finished.
*
*	@return int
*/
PostIt.prototype.daysToExpire = function(){
	var today = new Date();
	if(typeof this._expirationDate != "undefined"){
		var daysExpired = this.diffDays(today, this._expirationDate);

		if(daysExpired <= 0)
			return Math.abs(daysExpired);
		else
			return 0;
	}else
		return 0;
};

/**
*	Make the post-it expiring date be the actual date.
*/
PostIt.prototype.finalize = function () {
	this._finalizationDate = new Date();
};

/**
*	@private
*
*	Auxiliar method which return the difference (by number of days) of the two Dates given (the number can be negative).
*
*	@return int - difference of days
*/
PostIt.prototype.diffDays = function(date1, date2){

    var diff = Date.UTC(date1.getYear(), date1.getMonth(), date1.getDate(),0,0,0) - Date.UTC(date2.getYear(), date2.getMonth(), date2.getDate(),0,0,0);
		return (diff / 1000 / 60 / 60 / 24);
 
	};

